package com.idbk.chargestation.activity.user;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.PointListAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonPointsList;
import com.idbk.chargestation.bean.JsonPointsOverview;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;
import com.idbk.chargestation.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收藏（充电点）
 * @author lupc, zhx
 *
 */
public class ActivityMyFavorite extends BaseActivity implements OnClickListener{

	private final static String TAG = ActivityMyFavorite.class.getSimpleName();

	private Context mContext;

	private PullToRefreshListView mListView;

	public final static int PAGE_SIZE = 10;

	private int mPageIndex = 1;

	private List<JsonPointsOverview> mData;

	private PointListAdapter mAdapter; 
	
	private LinearLayout mLayoutEmpty, mLayoutError;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_myfavorite);

		setupView();

		setupData();
	}

	private void setupView(){
		mContext = this;

		setupToolBar2();//显示标题栏

		mLayoutEmpty = (LinearLayout)findViewById(R.id.tj_empty_view);
		mLayoutError = (LinearLayout)findViewById(R.id.tj_error_view);
		if (mLayoutError != null) {
			mLayoutError.setOnClickListener(this);
		}
		mData = new ArrayList<JsonPointsOverview>();
		mAdapter = new PointListAdapter(mContext,mData);
		mAdapter.setLongClickCancelCollection(true);
		mListView = (PullToRefreshListView)findViewById(R.id.pullToRefreshListView);
		if (mListView != null) {
			mListView.setMode(Mode.BOTH);
			mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
				@Override
				public void onPullDownToRefresh(
						PullToRefreshBase<ListView> refreshView) {
					//下拉的时候数据重置
					mMainRequest = ChargeStationAPI.getMyPointCollections(
							1, PAGE_SIZE, mReloadResponse);
				}
				@Override
				public void onPullUpToRefresh(
						PullToRefreshBase<ListView> refreshView) {
					//上拉的时候添加选项
					mMainRequest = ChargeStationAPI.getMyPointCollections(
							mPageIndex + 1, PAGE_SIZE, mLoadMoreResponse);
				}
			});
			mListView.setAdapter(mAdapter);
		}
	}

	private void setupData(){
		showMyProgressDialog(true,true,"加载中");
		mPageIndex = 1;	
		mMainRequest = ChargeStationAPI.getMyPointCollections(mPageIndex, PAGE_SIZE, mResponse);
	}

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonPointsList result = GsonUtils.toBean(JsonPointsList.class, response);
			if(handleResponseStatus(result)){
				mData.clear();
				mPageIndex = 1;
				
				if (result.points != null && result.points.size() > 0){
					mData.addAll(result.points);
					UIUtil.hideAll(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.VISIBLE);
					
				} else {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
				}
				mAdapter.notifyDataSetChanged();
				if(result.total > 99){
					setToolbar2Title("我的收藏(99+)");	
				} else {
					setToolbar2Title("我的收藏(" + result.total + ")");
				}				
			}
		}		

		@Override
		public void onEErrorResponse(VolleyError error) {
			Log.e(TAG, error.getMessage(), error.getCause());
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}	
	};

	private final EHttpResponse mReloadResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonPointsList result = GsonUtils.toBean(JsonPointsList.class, response);
			if(handleResponseStatus(result)){
				mData.clear();
				mPageIndex = 1;
				if(result.points != null && result.points.size() > 0){
					mData.addAll(result.points);					
				} 
				mAdapter.notifyDataSetChanged();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Log.e(TAG, error.getMessage(), error.getCause());
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			mListView.onRefreshComplete();
		}	
		
	};

	private final EHttpResponse mLoadMoreResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonPointsList result = GsonUtils.toBean(JsonPointsList.class, response);
			if(handleResponseStatus(result)){
				if(result.points != null && result.points.size() > 0){
					mPageIndex++;
					mData.addAll(result.points);
					mAdapter.notifyDataSetChanged();
				} else {
					Toast.makeText(mContext, R.string.common_tip_rusult_nomoredata,Toast.LENGTH_SHORT).show();
				}
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Log.e(TAG, error.getMessage(), error.getCause());	
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			mListView.onRefreshComplete();
		}	
		
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tj_error_view:
			setupData();
			break;
		default:
			break;
		}
	}

}
